﻿<?php
//Establish Session
/**
 * @author Invisible Man
 * @copyright 2013
 */
 //Checking Admin
 require('../includes/admin_auth.php');
//Callback all pages
require('../config.php');
//Before Enable
$page_title ='Danh sách Tin tức';
$script = array("function.js");
$menu_ex = array("news_add.php" => "Thêm Tin tức");
//Starting all pages
//Callback header
require('theme/default/header.php');
//Checking Error
if (isset($_GET['error'])) {
    echo '<p class="error_msg">Phát sinh lỗi khi xóa.</p>';
}
//Callback Content
echo '<table class="list_table">
    <tr>
        <td class="list_th">STT</td>
        <td class="list_th" style="width: 300px;">Tên tin tức</td>
        <td class="list_th" style="width: 100px;">Danh mục</td>
        <td class="list_th">Người đăng</td>
        <td class="list_th">Thời gian</td>
        <td class="list_th">Công khai</td>
        <td class="list_th" style="width: 50px;">Quản lý</td>
    </tr>';
    //Connect DB
    require('../includes/db_connect.php');


    //Query
    if (!isset($_GET['total'])) {
        $query_list = 'SELECT * FROM news ORDER BY news_id DESC';
        $result_list = mysql_query($query_list,$db_con);
        $data_row = mysql_num_rows($result_list);
        $total = $data_row;
    }
    else {
        $data_row = $_GET['total'];
        $total = $data_row;
    }
    
    /**
     * *********************************************
     * Page Navigate 
     ***********************************************/
     //Get pages
     if ($data_row % $max_page != 0) {
        $page = (int)($data_row/$max_page) + 1;
     } else {
        $page = (int)($data_row/$max_page);
    }
    //Checking Input from user
    if (isset($_GET['page']) && ($_GET['page'] <= $page)) {
        $page_get = $_GET['page'];
    } elseif (isset($_GET['page']) && ($_GET['page'] > $page)) {
        echo '<td colspan="7" class="align_center"><b style="color:red;">Không tìm thấy trang chỉ định</b></td>';
        $page_get = $page;
    } else {
        $page_get = 1;
    }
    //Rebuild Query
    $query_list = 'SELECT * FROM news as n, member as m, categories as c WHERE n.member_id=m.member_id AND n.cate_id=c.cate_id ORDER BY news_id DESC LIMIT ' . ($page_get -1)*$max_page . ', '.$max_page;
    $result_list = mysql_query($query_list,$db_con);
    $data_row = mysql_num_rows($result_list);

    
    /******************************************************/
    //Processing
    if ($data_row ==0) {
        echo '<td colspan="7"><p class ="align_center"><b style="color:red;">Chưa có tin tức nào trong Database</b></p></td>';
    }
    else {
        //Counting All Rows of Results
        if (!isset($_GET['page'])) {
            $count = 0;
        }
        else {
            $count = ($_GET['page'] - 1) *$max_page;
        }
        while ($data = mysql_fetch_assoc($result_list)) {
            $count++;
            echo '<tr>
            <td class="list_td">' .$count. '</td>
            <td class="list_td">' .$data['news_title']. '</td>
            <td class="list_td">' .$data['cate_name']. '</td>
            <td class="list_td">' .$data['member_user']. '</td>
            <td class="list_td">' .date("d/m/Y", $data['news_date'] + $location_time*3600). '</td>
            <td class="list_td">'; 
                if ($data['news_publish'] == "1") echo 'Yes';
                else echo 'No';
            echo'</td>
            <td class="list_td">
            <a href="news_edit.php?id=' .$data['news_id']. '&cate_id=' .$data['cate_id']. '"><img src="images/edit.png" style="vertical-align:middle;" onclick ="" /></a>&nbsp&nbsp&nbsp
            <a href="news_del.php?id=' .$data['news_id']. '"><img src="images/delete.png" style="vertical-align:middle;" onclick ="return user_confirm(\'Bạn có chắc là muốn xóa nó không?\');" /></a></td>
        </tr>';
        }
    }

    echo '
</table>';
    
    /**
     * *************************************
     * PAGE NAV SHOW
     * *************************************/
    $page_next = 1;
    if (isset($_GET['page']) && isset($page_count)) {
        $page_next = $_GET['page'];
    }
    $page_prev = $page_next -1;
    if (isset($page) && $page > 1) {
    echo '
        <div id="page_nav">
            <a href="'; 
                echo $_SERVER['PHP_SELF'];
            echo '">Đầu tiên</a>
            <a href="'; 
            if ($page_get > 1) {
                $page_prev = $page_get -1;
                echo $_SERVER['PHP_SELF']. '?page=' . $page_prev . '&total=' .$total;
            } else {
                echo $_SERVER['PHP_SELF']. '?page=' . $page_get . '&total=' .$total;
            }
            echo '">Trước</a>';
            
            /**
             * PAGE SPLIT
             * */
             $page_list = $max_page_list;
             $page_center = ceil($max_page_list/2);
             $page_step = $max_page_list - $page_center;
             $page_current = $page_get;
             $start = $page_current - $page_step;
             $end = $page_current + $page_step;
             if ($end > $page) {
                $end = $page;
             }
             //echo $start . '  ' . $end;
             for ($start;$start <= $end;$start++) {
                if ($start > 0 && $start <= $page) {
                    if ($start == $page_get) {
                        echo  '<span class="active">' .$start. '</span>';
                    } else {
                        echo  '<a href="' .$_SERVER['PHP_SELF']. '?page=' . $start. '&total=' .$total. '">' .$start. '</a>';
                    }
                    
                }
                
             }
             

            /*********************************************************/
            echo '<a href="'; 
            if ($page_get < $page) {
                $page_next = $page_get +1;
                echo $_SERVER['PHP_SELF']. '?page=' .$page_next. '&total=' .$total;
            } else {
                $page_next = $page_get;
                echo $_SERVER['PHP_SELF']. '?page=' .$page_next. '&total=' .$total;
            }
            echo'">Sau</a>
            <a href="'; 
                echo $_SERVER['PHP_SELF']. '?page=' . $page. '&total=' .$total;      
            echo '">Cuối cùng</a>
        </div>';
        }
    /////////////////////////////////////////////////////
    
    
//Callback Footer
require('theme/default/footer.php');
?>